<template>
  <view class="quick-functions">
    <view 
      v-for="func in safeFunctions" 
      :key="func.id" 
      class="function-item"
      hover-class="item-hover"
      @click="onSelect(func)"
    >
      <view class="function-icon">
        <!-- 使用条件渲染，根据图标类型显示不同的图片 -->
        <uni-icons v-if="iconExists(func.icon)" :type="func.icon" size="24" color="#ff00ff"></uni-icons>
        <image 
          v-else 
          :src="getIconSrc(func.icon)" 
          class="custom-icon-image" 
          mode="aspectFit"
        ></image>
      </view>
      <text class="function-title">{{ func.title }}</text>
    </view>
  </view>
</template>

<script setup>
import { computed } from 'vue';

// 定义props
const props = defineProps({
  functions: {
    type: Array,
    default: () => []
  }
});

// 确保functions数组中的每个项目都具有必要的属性
const safeFunctions = computed(() => {
  return props.functions.map(func => ({
    id: func.id || Math.random().toString(36).substr(2, 9),
    title: func.title || '未命名功能',
    icon: func.icon || 'info'
  }));
});

// 定义事件
const emit = defineEmits(['select']);

// 选择功能方法
const onSelect = (func) => {
  emit('select', func);
};

// 检查图标是否存在于uni-icons中
const iconExists = (iconName) => {
  // uni-icons中存在的图标
  const existingIcons = ['help', 'info', 'checkmark-circle', 'calendar'];
  return existingIcons.includes(iconName);
};

// 获取自定义图标的路径
const getIconSrc = (iconName) => {
  // 图标映射表
  const iconMap = {
    'wifi': '/static/icons/wifi.png',
    'sync': '/static/icons/link.png',
    // 可以添加更多图标映射
  };
  
  return iconMap[iconName] || '/static/icons/default-icon.png';
};
</script>

<style>
.quick-functions {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  width: 100%;
  box-sizing: border-box;
}

.function-item {
  width: 48%;
  background-color: rgba(0, 0, 0, 0.3);
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  border: 1px solid rgba(255, 0, 255, 0.3);
  box-sizing: border-box;
}

.item-hover {
  background-color: rgba(255, 0, 255, 0.1);
  transform: scale(1.02);
}

.function-icon {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 10px;
  border: 1px solid rgba(255, 0, 255, 0.5);
}

.custom-icon-image {
  width: 24px;
  height: 24px;
}

.function-title {
  font-size: 14px;
  color: #ffffff;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}
</style>
